//
//  XSTabBarController.swift
//  SwiftProject
//
//  Created by 杨晓生 on 16/8/3.
//  Copyright © 2016年 XS. All rights reserved.
//

import UIKit

class XSTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加子控制器
        addChildViewControllers()
        
        // 此时 tabBarButton 还没有创建
//        print(tabBar.subviews)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
//        print(tabBar.subviews)
        // 初始化加号按钮（要计算加号按钮的位置就要先知道其他tabBar子控件的位置）
        setupComposeBtn()
    }
    
    // MARK: - 内部控制方法
    /**
    设置加号按钮位置
    */
    private func setupComposeBtn() {
        tabBar.addSubview(composeBtn)
        let width = tabBar.bounds.width / CGFloat((viewControllers?.count)!)
        let rect = CGRect(x: 0, y: 0, width: width, height: tabBar.bounds.height)
         // 设置按钮frame和偏移位
        composeBtn.frame = CGRectOffset(rect, 2 * width, 0)
    }
    
    //加号按钮
    private lazy var composeBtn: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "composeBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    func composeBtnClick(){
        let composeVC = XSComposeViewController()
        self.presentViewController(composeVC, animated: true) { () -> Void in
            
        }
    }
    
    //添加子控制器
    func addChildViewControllers(){
        
        initChildViewController(XSHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        initChildViewController(XSMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        initChildViewController(XSComposeViewController(), title: "", imageName: "")
        initChildViewController(XSDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        initChildViewController(XSMeTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    /**
    *  初始化子控制器
    */
    func initChildViewController(childController: UIViewController, title: String, imageName: String){

        // 从内像外设置, nav和tabbar都有
        childController.title = title;
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        // 注意: Xocde7之前只有文字有效果, 还需要设置图片渲染模式
        tabBar.tintColor = UIColor.orangeColor()
        
        // 2.创建导航控制
        let nav = XSNavigationController()
        nav.addChildViewController(childController)
        
        // 3.添加控制器到tabbarVC
        addChildViewController(nav)
    }
    
}
